nanopyx.core.transform.parameter_sweep

 1from nanopyx.core.transform.new_error_map import ErrorMap
 2from nanopyx.core.analysis.frc import FIRECalculator
 3from nanopyx.core.transform.sr_radial_gradient_convergence import RadialGradientConvergence as RGC
 4from nanopyx.core.transform.sr_temporal_correlations import calculate_eSRRF_temporal_correlations
 5import numpy as np
 6
 7class ParameterSweep:
 8
 9    def __init__(self, doErrorMapping: bool = True, doFRCMapping: bool = True):
10
11        self.doErrorMapping = doErrorMapping
12        self.doFRCMapping = doFRCMapping
13    
14    # check for image dimensions in the method
15    def run(self, im: np.array, sensitivity_array: np.array, radius_array: np.array, temporal_correlation: str = "AVG"):
16
17        RSP_map = np.zeros((len(sensitivity_array), len(radius_array)))
18        FRC_map = np.zeros((len(sensitivity_array), len(radius_array)))
19        s_size = len(sensitivity_array)
20        r_size = len(radius_array)
21
22        for s in range(s_size):
23            for r in range(r_size):
24                rgc =  RGC(radius = radius_array[r], sensitivity = sensitivity_array[s])
25                if self.doErrorMapping:
26                    rgc_map = rgc.calculate(im)[0]
27                    reconstruction = calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation)
28                    RSP_map[s,r] = self.calculate_rsp(im, reconstruction)
29                if self.doFRCMapping:
30                    rgc_map_odd = rgc.calculate(im[1::2,:,:])[0]
31                    rgc_map_even = rgc.calculate(im[::2,:,:])[0]
32                    reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
33                    reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
34                    FRC_map[s,r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
35
36        QnR = self.calculate_qnr_score(RSP_map, FRC_map)
37        
38        return QnR
39    
40    def calculate_rsp(self, im, reconstruction):
41        error_map = ErrorMap()
42        error_map.optimise(np.mean(im, axis=0), reconstruction)
43        return error_map.getRSP()
44    
45    def calculate_frc(self, im_odd, im_even):
46        frc_calculator = FIRECalculator(pixel_size=20, units="nm")
47        fire_nb = frc_calculator.calculate_fire_number(np.asarray(im_odd), np.asarray(im_even))
48        return fire_nb
49
50    def logistic_image_conversion(self, im, min_val = 50, max_val = 200): # max and min in nm
51    
52        M1 = 0.075
53        M2 = 0.925
54        A1 = np.log((1 - M1) / M1)
55        A2 = np.log((1 - M2) / M2)
56        x0 = (A2 * max_val - A1 * min_val) / (A2 - A1)
57        k = 1 / (x0 - max_val) * A1
58
59        im_out = []
60        for image in im:
61            normalized_image = 1 / (np.exp(-k * (image - x0)) + 1)
62            im_out.append(normalized_image)
63
64        return np.asarray(im_out)
65    
66    def calculate_qnr_score(self, RSP: np.array, FRC: np.array):
67        assert RSP.shape == FRC.shape
68        nFRC = self.logistic_image_conversion(FRC)
69        QnR = (2 * np.asarray(RSP) * np.asarray(nFRC)) / (np.asarray(RSP) + np.asarray(nFRC)) 
70        return QnR
71    
class ParameterSweep:
 8class ParameterSweep:
 9
10    def __init__(self, doErrorMapping: bool = True, doFRCMapping: bool = True):
11
12        self.doErrorMapping = doErrorMapping
13        self.doFRCMapping = doFRCMapping
14    
15    # check for image dimensions in the method
16    def run(self, im: np.array, sensitivity_array: np.array, radius_array: np.array, temporal_correlation: str = "AVG"):
17
18        RSP_map = np.zeros((len(sensitivity_array), len(radius_array)))
19        FRC_map = np.zeros((len(sensitivity_array), len(radius_array)))
20        s_size = len(sensitivity_array)
21        r_size = len(radius_array)
22
23        for s in range(s_size):
24            for r in range(r_size):
25                rgc =  RGC(radius = radius_array[r], sensitivity = sensitivity_array[s])
26                if self.doErrorMapping:
27                    rgc_map = rgc.calculate(im)[0]
28                    reconstruction = calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation)
29                    RSP_map[s,r] = self.calculate_rsp(im, reconstruction)
30                if self.doFRCMapping:
31                    rgc_map_odd = rgc.calculate(im[1::2,:,:])[0]
32                    rgc_map_even = rgc.calculate(im[::2,:,:])[0]
33                    reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
34                    reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
35                    FRC_map[s,r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
36
37        QnR = self.calculate_qnr_score(RSP_map, FRC_map)
38        
39        return QnR
40    
41    def calculate_rsp(self, im, reconstruction):
42        error_map = ErrorMap()
43        error_map.optimise(np.mean(im, axis=0), reconstruction)
44        return error_map.getRSP()
45    
46    def calculate_frc(self, im_odd, im_even):
47        frc_calculator = FIRECalculator(pixel_size=20, units="nm")
48        fire_nb = frc_calculator.calculate_fire_number(np.asarray(im_odd), np.asarray(im_even))
49        return fire_nb
50
51    def logistic_image_conversion(self, im, min_val = 50, max_val = 200): # max and min in nm
52    
53        M1 = 0.075
54        M2 = 0.925
55        A1 = np.log((1 - M1) / M1)
56        A2 = np.log((1 - M2) / M2)
57        x0 = (A2 * max_val - A1 * min_val) / (A2 - A1)
58        k = 1 / (x0 - max_val) * A1
59
60        im_out = []
61        for image in im:
62            normalized_image = 1 / (np.exp(-k * (image - x0)) + 1)
63            im_out.append(normalized_image)
64
65        return np.asarray(im_out)
66    
67    def calculate_qnr_score(self, RSP: np.array, FRC: np.array):
68        assert RSP.shape == FRC.shape
69        nFRC = self.logistic_image_conversion(FRC)
70        QnR = (2 * np.asarray(RSP) * np.asarray(nFRC)) / (np.asarray(RSP) + np.asarray(nFRC)) 
71        return QnR
ParameterSweep(doErrorMapping: bool = True, doFRCMapping: bool = True)
10    def __init__(self, doErrorMapping: bool = True, doFRCMapping: bool = True):
11
12        self.doErrorMapping = doErrorMapping
13        self.doFRCMapping = doFRCMapping
doErrorMapping
doFRCMapping
def run( self, im: <built-in function array>, sensitivity_array: <built-in function array>, radius_array: <built-in function array>, temporal_correlation: str = 'AVG'):
16    def run(self, im: np.array, sensitivity_array: np.array, radius_array: np.array, temporal_correlation: str = "AVG"):
17
18        RSP_map = np.zeros((len(sensitivity_array), len(radius_array)))
19        FRC_map = np.zeros((len(sensitivity_array), len(radius_array)))
20        s_size = len(sensitivity_array)
21        r_size = len(radius_array)
22
23        for s in range(s_size):
24            for r in range(r_size):
25                rgc =  RGC(radius = radius_array[r], sensitivity = sensitivity_array[s])
26                if self.doErrorMapping:
27                    rgc_map = rgc.calculate(im)[0]
28                    reconstruction = calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation)
29                    RSP_map[s,r] = self.calculate_rsp(im, reconstruction)
30                if self.doFRCMapping:
31                    rgc_map_odd = rgc.calculate(im[1::2,:,:])[0]
32                    rgc_map_even = rgc.calculate(im[::2,:,:])[0]
33                    reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
34                    reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
35                    FRC_map[s,r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
36
37        QnR = self.calculate_qnr_score(RSP_map, FRC_map)
38        
39        return QnR
def calculate_rsp(self, im, reconstruction):
41    def calculate_rsp(self, im, reconstruction):
42        error_map = ErrorMap()
43        error_map.optimise(np.mean(im, axis=0), reconstruction)
44        return error_map.getRSP()
def calculate_frc(self, im_odd, im_even):
46    def calculate_frc(self, im_odd, im_even):
47        frc_calculator = FIRECalculator(pixel_size=20, units="nm")
48        fire_nb = frc_calculator.calculate_fire_number(np.asarray(im_odd), np.asarray(im_even))
49        return fire_nb
def logistic_image_conversion(self, im, min_val=50, max_val=200):
51    def logistic_image_conversion(self, im, min_val = 50, max_val = 200): # max and min in nm
52    
53        M1 = 0.075
54        M2 = 0.925
55        A1 = np.log((1 - M1) / M1)
56        A2 = np.log((1 - M2) / M2)
57        x0 = (A2 * max_val - A1 * min_val) / (A2 - A1)
58        k = 1 / (x0 - max_val) * A1
59
60        im_out = []
61        for image in im:
62            normalized_image = 1 / (np.exp(-k * (image - x0)) + 1)
63            im_out.append(normalized_image)
64
65        return np.asarray(im_out)
def calculate_qnr_score(self, RSP: <built-in function array>, FRC: <built-in function array>):
67    def calculate_qnr_score(self, RSP: np.array, FRC: np.array):
68        assert RSP.shape == FRC.shape
69        nFRC = self.logistic_image_conversion(FRC)
70        QnR = (2 * np.asarray(RSP) * np.asarray(nFRC)) / (np.asarray(RSP) + np.asarray(nFRC)) 
71        return QnR